package com.yezao.baseui.activities

import android.app.Dialog
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.FrameLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationManagerCompat
import com.yezao.baseui.R
import com.yezao.baseui.sequenceableDialog.SequenceDialogManager
import com.yezao.baseui.widget.errorview.BaseErrorView
import com.yezao.baseui.widget.loadingview.BaseLoadingView

abstract class BaseYZActivity : AppCompatActivity(), BaseLoadingView.LoadingListener {

    protected val TAG = javaClass.simpleName
    protected open fun getContentLayout(): Int {
        return -1
    }

    protected fun getErrorLayout() = R.layout.default_error_layout
    protected fun getLoadingLayout() = R.layout.default_loading_layout


    private lateinit var mContentLayout: FrameLayout
    private lateinit var mErrorLayout: FrameLayout
    private lateinit var mLoadingLayout: FrameLayout
    private lateinit var mWidgetLayout: FrameLayout

    private lateinit var mContentView: View
    private lateinit var mErrorView: BaseErrorView
    private lateinit var mLoadingView: BaseLoadingView

    private lateinit var dialogManager: SequenceDialogManager

    protected val uiHandler = Handler(Looper.getMainLooper())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.base_root_layout)
        mErrorLayout = findViewById<FrameLayout>(R.id.errro_container)
        mLoadingLayout = findViewById<FrameLayout>(R.id.loading_container)
        mContentLayout = findViewById<FrameLayout>(R.id.main_container)
        mWidgetLayout = findViewById(R.id.widget_container)
        if (getContentLayout() > 0) {
            mContentView = layoutInflater.inflate(getContentLayout(), mContentLayout)
                .findViewById<View>(getContentLayout())
            mContentLayout.visibility = View.VISIBLE
        }

        if (getErrorLayout() > 0) {
            mErrorView = layoutInflater.inflate(getErrorLayout(), mErrorLayout)
                .findViewById(getErrorLayout())
            mErrorLayout.visibility = View.GONE
        }
        if (getLoadingLayout() > 0) {
            mLoadingView = layoutInflater.inflate(getLoadingLayout(), mLoadingLayout)
                .findViewById(getLoadingLayout())
            mLoadingLayout.visibility = View.GONE
            mLoadingView.setLoadingListener(this)
        }
        dialogManager = SequenceDialogManager(this)
    }


    protected fun onRefresh() {}
    protected fun onLoadmore() {}
    protected fun canRefresh() = false
    protected fun canLoadMore() = false


    //region 队列dialog
    protected fun addDialog(dialog: Dialog) {
        dialogManager.add(dialog)
    }

    protected fun showNextDialog() {
        if (dialogManager.hasNext()) {
            dialogManager.popNext()
        }
    }
    //endregion

    //region toast控制
    /**
     * 当应用没有notification 权限时 无法弹出toast
     * */
    protected fun showToast(msg: String?, l: Int = Toast.LENGTH_SHORT) {
        //默认使用系统toast
        if (NotificationManagerCompat.from(this).areNotificationsEnabled()) {
            Toast.makeText(this, msg, l).show()
        } else {//如果没有notification 权限 则使用布局内的备用

        }

    }
    //endregion

    //region Loading view
    protected fun showLoading() {
        mLoadingLayout.visibility = View.VISIBLE
        mLoadingView.startLoading()
    }

    override fun onStartAnimFinished() {

    }

    protected fun hideLoading() {
        mLoadingView.stopLoading()
    }

    override fun onStopAnimFinished() {
        mLoadingLayout.visibility = View.GONE
    }
    //endregion

    //region 错误界面
    protected fun showError() {}


    protected fun hideError() {}
    //endregion


}